﻿using DatabaseCore.Modal;
using DatabaseCore.Sqlite;
using NW_WEB_Service.App.Interface;
using NW_WEB_Service.com.app.entities.modal;
using Service.src.com.utils.modal;
using Service.src.Flow.Server;
using Service.src.NW.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace NW_WEB_Service.com.app.controllers
{
    [RoutePrefix("api/data-manage")]
    public class DataManageController : ApiController
    {
        public DataManageInterface DataManageService;

        public DataManageController()
        {
            DataManageService = new DataManageService();
        }
        /// <summary>
        /// 查找数据库中的所有表表名
        /// </summary>
        /// <returns></returns>
        [HttpGet()]
        [Route("table-name")]
        public HttpResult FindTableNames()
        {
            List<SystemTable> systemTables = new DataManageService().FindTableNames(SqliteHelper.SystemDefaultDBID);
            if (systemTables.Count > 0)
            {
                systemTables = systemTables.Where(s =>
                {
                    bool pa = !AppData.IgnoreSystemTables.Contains(s.name);
                    if (pa)
                    {
                        foreach (string item in AppData.IgnoreSystemTablePrefix)
                        {
                            if (!pa)
                            {
                                break;
                            }
                            pa = !s.name.StartsWith(item);
                        }
                    }
                    return pa;
                }).ToList();
            }
            return new HttpResult().Success(systemTables);
        }

        /// <summary>
        /// 获取表结构
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("table-structure")]
        public HttpResult FindTableStructure(string TableName)
        {
            List<TableStructure> TableStructures = DataManageService.FindTableStructures(TableName);
            if (TableStructures.Count <= 0)
            {
                Console.WriteLine("1");
            }
            return new HttpResult().Success(TableStructures);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="Sql"></param>
        /// <param name="TableName"></param>
        /// <param name="Page"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("query-by-sql")]
        public HttpResult QueryBySql([FromBody] SqlDTO QueryBySql)
        {
            DBResult Res = DataManageService.QueryBySql(QueryBySql.Sql);
            return new HttpResult().Success(Res);
        }

        /// <summary>
        /// 分页查询数据
        /// </summary>
        /// <param name="Sql"></param>
        /// <param name="TableName"></param>
        /// <param name="Page"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("page-query-by-sql")]
        public HttpResult PageQueryBySql([FromBody] SqlDTO Sql)
        {
            DBResult Res = DataManageService.PageQueryBySql(Sql.Sql, Sql.TableName, Sql.Page, Sql.Size);
            return new HttpResult().Success(Res);
        }

        /// <summary>
        /// 执行SQL
        /// 用在新增，删除，修改等
        /// </summary>
        /// <param name="Sql"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("execute-sql")]
        public HttpResult ExecuteSql([FromBody] SqlDTO Sql)
        {
            int Res = new DataManageService().ExecuteSql(SqliteHelper.SystemDefaultDBID, Sql.Sql);
            return new HttpResult().Success(Res);
        }

    }
}
